package com.iqiyi.pps.epg.api.server.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.mams.hedvig.rpc.client.ServiceHelper;
import com.iqiyi.pps.epg.api.Server;
import com.iqiyi.pps.epg.api.aop.annotation.SkipAuthCheck;
import com.iqiyi.pps.epg.api.aop.annotation.SkipInterfaceLog;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.sginal.*;
import com.iqiyi.pps.epg.api.server.web.sginal.SginalWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.ws.rs.FormParam;
import java.util.*;

/**
 * Created by zhang on 2017/6/12.
 */
@Service
public class SginalProxy extends BaseProxy {
    private Logger logger = LoggerFactory.getLogger(SginalProxy.class);

    public String createSginal(String entity) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(entity)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            JSONObject jsonObject = JSON.parseObject(entity);
            TSginalBase thriftModel = new TSginalBase();
            thriftModel.setName(jsonObject.getString("name"));
            thriftModel.setPort(jsonObject.getString("port"));
            thriftModel.setAccessServerIp(jsonObject.getString("accessServerIp"));
            thriftModel.setStatus(TSginalConstants.STATUS_INIT);
            thriftModel.setAuthor(jsonObject.getString("author"));
            thriftModel.setIsDelete(TSginalConstants.IS_NOT_DELETE);
            JSONArray jsonArray = jsonObject.getJSONArray("sginalServers");
            for(int i=0;i<jsonArray.size();i++){
                JSONObject sginalServer = jsonArray.getJSONObject(i);
                sginalServer.put("isDelete", TSginalConstants.IS_NOT_DELETE);
            }
            thriftModel.setSginalServers(jsonArray.toJavaList(TSginalServer.class));

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.modifySginal(thriftModel, ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "添加成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[createSginal error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String editSginal(String entity) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(entity)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            JSONObject jsonObject = JSON.parseObject(entity);
            TSginalBase thriftModel = new TSginalBase();
            thriftModel.setId(jsonObject.getInteger("id"));
            thriftModel.setName(jsonObject.getString("name"));
            thriftModel.setPort(jsonObject.getString("port"));
            thriftModel.setAccessServerIp(jsonObject.getString("accessServerIp"));
            thriftModel.setAuthor(jsonObject.getString("author"));
            thriftModel.setSginalServers(jsonObject.getJSONArray("sginalServers").toJavaList(TSginalServer.class));
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.modifySginal(thriftModel, ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[editSginal error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String deleteSginal(String id) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(id)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.deleteSginal(Integer.parseInt(id), ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "删除成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[deleteSginal error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String forceDeleteSginal(String id) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(id)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.forceDeleteSginal(Integer.parseInt(id), ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "强制删除成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String startSginal(String id) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(id)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.startSginal(Integer.parseInt(id), ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "开启成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[startSginal error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String stopSginal(String id) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(id)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.stopSginal(Integer.parseInt(id), ServiceUtil.getOpUser());
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "停止成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[EPG][API][stopSginal][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String sginalList(Integer pageNo,
                             Integer pageSize,
                             String id,
                             String name,
                             String port,
                             String status,
                             String author){
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(id)) {
                params.put("EQI_id", id);
            }
            if (StringUtils.isNotEmpty(name)) {
                params.put("LIKES_name", name);
            }
            if (StringUtils.isNotEmpty(port)) {
                params.put("LIKES_port", port);
            }
            if (StringUtils.isNotEmpty(status)) {
                params.put("EQI_status", status);
            }
            if (StringUtils.isNotEmpty(author)) {
                params.put("LIKES_author", author);
            }
            if ("all".equals(status)) {
                params.remove("EQI_status");
            }
            params.put("NEQI_isDelete", "1");
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TSginalDataPage tSginalDataPage = service.getSginalData(page, tFilter);
            if (tSginalDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tSginalDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[sginalList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
    }

    public String createServer(String entity) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(entity)) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }
            JSONObject jsonObject = JSON.parseObject(entity);
            com.iqiyi.pps.epg.api.model.web.sginal.TServer thriftModel = new com.iqiyi.pps.epg.api.model.web.sginal.TServer();
            thriftModel.setSn(jsonObject.getString("sn"));
            thriftModel.setInsideServerIp(jsonObject.getString("insideServerIp"));
            thriftModel.setOutsideServerIp(jsonObject.getString("outsideServerIp"));
            thriftModel.setPort(jsonObject.getString("port"));
            thriftModel.setProvince(jsonObject.getString("province"));
            thriftModel.setCity(jsonObject.getString("city"));
            thriftModel.setStatus(jsonObject.getInteger("status"));
            thriftModel.setServerType(jsonObject.getInteger("serverType"));
            thriftModel.setIdc(jsonObject.getString("idc"));
            thriftModel.setSystem(jsonObject.getString("system"));
            thriftModel.setVersion(jsonObject.getString("version"));
            thriftModel.setHardware(jsonObject.getString("hardware"));
            thriftModel.setRemark(jsonObject.getString("remark"));

            thriftModel.setIncluding(Integer.parseInt(jsonObject.getString("including")));

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.createServer(thriftModel);
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "添加成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }

        } catch (Exception e) {
            logger.error("[createServer error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String editServer(String entity) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(entity)) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }
            JSONObject jsonObject = JSON.parseObject(entity);
            com.iqiyi.pps.epg.api.model.web.sginal.TServer thriftModel = new com.iqiyi.pps.epg.api.model.web.sginal.TServer();
            thriftModel.setId(jsonObject.getInteger("id"));
            thriftModel.setSn(jsonObject.getString("sn"));
            thriftModel.setInsideServerIp(jsonObject.getString("insideServerIp"));
            thriftModel.setOutsideServerIp(jsonObject.getString("outsideServerIp"));
            thriftModel.setPort(jsonObject.getString("port"));
            thriftModel.setProvince(jsonObject.getString("province"));
            thriftModel.setCity(jsonObject.getString("city"));
            thriftModel.setStatus(jsonObject.getInteger("status"));
            thriftModel.setServerType(jsonObject.getInteger("serverType"));
            thriftModel.setIdc(jsonObject.getString("idc"));
            thriftModel.setSystem(jsonObject.getString("system"));
            thriftModel.setVersion(jsonObject.getString("version"));
            thriftModel.setHardware(jsonObject.getString("hardware"));
            thriftModel.setRemark(jsonObject.getString("remark"));

            thriftModel.setIncluding(Integer.parseInt(jsonObject.getString("including")));


            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.updateServer(thriftModel);
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[EPG][API][editServer][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String deleteServer(String id) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(id)) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.deleteServer(Integer.parseInt(id));
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, entityId, "删除成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "删除失败");
            }
        } catch (Exception e) {
            logger.error("[deleteServer error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String serverList(Integer pageNo,
                             Integer pageSize,
                             String id,
                             String sn,
                             String insideServerIp,
                             String outsideServerIp,
                             String port,
                             String status,
                             String serverType,
                             String province,
                             String city,
                             String idc,
                             String system,
                             String version,
                             String hardware,
                             String load,
                             String remark,
                             String dataFree,String dataUsed,String allFree,String allUsed,String allMem,String availableMem,String allSwap,String freeSwap,String icmploss,
                             String monitor,String including, String cluster,
                             String orderBy,
                             String sort) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(id)) {
                params.put("EQI_id", id);
            }
            if (StringUtils.isNotEmpty(sn)) {
                params.put("LIKES_sn", sn);
            }
            if (StringUtils.isNotEmpty(insideServerIp)) {
                params.put("LIKES_insideServerIp", insideServerIp);
            }
            if (StringUtils.isNotEmpty(outsideServerIp)) {
                params.put("LIKES_outsideServerIp", outsideServerIp);
            }
            if (StringUtils.isNotEmpty(port)) {
                params.put("LIKES_port", port);
            }
            if (StringUtils.isNotEmpty(status)) {
                params.put("EQI_status", status);
            } else {
                params.put("NEQI_status", "-1");
            }
            if (StringUtils.isNotEmpty(serverType)) {
                params.put("EQI_serverType", serverType);
            }
            if (StringUtils.isNotEmpty(province)) {
                params.put("EQS_province", province);
            }
            if (StringUtils.isNotEmpty(city)) {
                params.put("EQS_city", city);
            }
            if (StringUtils.isNotEmpty(idc)) {
                params.put("EQS_idc", idc);
            }
            if (StringUtils.isNotEmpty(system)) {
                params.put("LIKES_system", system);
            }
            if (StringUtils.isNotEmpty(version)) {
                params.put("LIKES_version", version);
            }
            if (StringUtils.isNotEmpty(hardware)) {
                params.put("LIKES_hardware", hardware);
            }

            if (StringUtils.isNotEmpty(load)) {
                params.put("GTN_load", load);
            }

            if (StringUtils.isNotEmpty(remark)) {
                params.put("LIKES_remark", remark);
            }

            if (StringUtils.isNotEmpty(dataFree)) {
                params.put("GTS_dataFree", dataFree);
            }

            if (StringUtils.isNotEmpty(dataUsed)) {
                params.put("GTS_dataUsed", dataUsed);
            }

            if (StringUtils.isNotEmpty(allFree)) {
                params.put("GTS_allFree", allFree);
            }

            if (StringUtils.isNotEmpty(allUsed)) {
                params.put("GTS_allUsed", allUsed);
            }

            if (StringUtils.isNotEmpty(allMem)) {
                params.put("GTS_allMem", allMem);
            }

            if (StringUtils.isNotEmpty(monitor)) {
                if("error".equals(monitor)){
                    params.put("NEQI_monitor", monitor);
                }else{
                    params.put("EQI_monitor", monitor);
                }
            }

            if (StringUtils.isNotEmpty(cluster)) {
                params.put("EQI_cluster", cluster);

            }

            if (StringUtils.isNotEmpty(including)) {
                params.put("EQI_including", including);

            }

            if (StringUtils.isNotEmpty(availableMem)) {
                params.put("GTS_availableMem", availableMem);
            }
            if (StringUtils.isNotEmpty(allSwap)) {
                params.put("GTS_allSwap", allSwap);
            }
            if (StringUtils.isNotEmpty(freeSwap)) {
                params.put("GTN_freeSwap", freeSwap);
            }
            if (StringUtils.isNotEmpty(icmploss)) {
                params.put("EQS_icmploss", icmploss);
            }

            if ("all".equals(status)) {
                params.remove("EQI_status");
                params.put("NEQI_status", "-1");
            }
            if ("all".equals(serverType)) {
                params.remove("EQI_serverType");
            }
            if ("all".equals(province)) {
                params.remove("EQS_province");
            }
            if ("all".equals(city)) {
                params.remove("EQS_city");
            }
            if ("all".equals(idc)) {
                params.remove("EQS_idc");
            }
            if ("all".equals(monitor)) {
                params.remove("EQI_monitor");
            }

            if ("all".equals(cluster)) {
                params.remove("EQI_cluster");
            }



            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = null;
            if(StringUtils.isNotEmpty(sort)&&StringUtils.isNotEmpty(orderBy)){
                if(sort.equals("asc")||sort.equals("desc")){
                    page = new TPage(-1, pageNo, pageSize, orderBy, sort, true);
                }else{
                    page = new TPage(-1, pageNo, pageSize, "updateTime", "desc", true);
                }
            }else {
                page = new TPage(-1, pageNo, pageSize, "updateTime", "desc", true);
            }


            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TServerDataPage tServerDataPage = service.getServersData(page, tFilter);
            if (tServerDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tServerDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[serverList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, e, "发现异常");
            return jsonStrResp;
        }
    }

    @SkipAuthCheck
	@SkipInterfaceLog
    public String sginalStatus(String cid, String status) {
        String jsonStrResp = "";
        try {
            if ( StringUtils.isEmpty(status) || cid == null) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            long entityId = service.sginalStatus(Integer.parseInt(cid), status);
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "获取成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr((int) entityId));
            }

        } catch (Exception e) {
            logger.error("[sginalStatus error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String sginalStatusList(Integer pageNo, Integer pageSize, String id, String fatherId, String serverIp,String startDate,String endDate) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(id)) {
                params.put("EQL_id", id);
            }
            if (StringUtils.isNotEmpty(fatherId)) {
                params.put("EQI_fatherId", fatherId);
            }
            if (StringUtils.isNotEmpty(serverIp)) {
                params.put("EQS_serverIp", serverIp);
            }

            if (StringUtils.isNotEmpty(startDate)) {
                params.put("GES_addTime", startDate);
            }

            if (StringUtils.isNotEmpty(endDate)) {
                params.put("LES_addTime", endDate);
            }

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "addTime", "desc", true);
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TSginalStatusDataPage tSginalStatusDataPage = service.getSginalStatusData(page, tFilter);
            if (tSginalStatusDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tSginalStatusDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[sginalStatusList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
    }

    @SkipInterfaceLog
    public String sginalLogList(Integer pageNo, Integer pageSize, String fatherId) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(fatherId)) {
                params.put("EQI_fatherId", fatherId);
            }

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TSginalLogDataPage tSginalLogDataPage = service.getSginalLogData(page, tFilter);
            if (tSginalLogDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tSginalLogDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[sginalLogList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }

    }

    @SkipInterfaceLog
    public String serverStatusList(Integer pageNo, Integer pageSize, String serverId) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(serverId)) {
                params.put("EQL_serverId", serverId);
            }

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TServerStatusDataPage tServerStatusDataPage = service.getServerStatusData(page, tFilter);
            if (tServerStatusDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tServerStatusDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[serverStatusList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, e, "发现异常");
            return jsonStrResp;
        }

    }

    @SkipInterfaceLog
    public String zzdServerList(String paramsObj, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 100 : pageSize;
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            Map<String, String> params = new HashMap<>();

            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            //params.put("EQI_serverStatus", "0");
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "updateTime", "desc", true);
            TVersionControllerDataPage tVersionControllerDataPage = service.getVersionControllerData(page, tFilter);
            if (tVersionControllerDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVersionControllerDataPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getZzdServerList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String versionHistory() {
        String jsonStrResp = "";
        try {
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            List<String> history = service.getVersionHistory();
            if(history!=null){
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, history, "操作成功").toJsonString();
            }else{
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            }
        }catch(Exception e){
            logger.error("[versionHistory][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String delVersionController(String serverId) {
        String jsonStrResp = "";
        try {
            if(serverId == null){
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
                return jsonStrResp;
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.delVersionController(Integer.parseInt(serverId));
            if(res>0){
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, serverId, "操作成功").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, serverId, "操作失败").toJsonString();
            }
        }catch(Exception e){
            logger.error("[delVersionController][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }

        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String synVersion(String insideServerIp,String hardware,String remark) {
        String jsonStrResp = "";
        try {
            if(insideServerIp == null){
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
                return jsonStrResp;
            }
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.synVersionController(insideServerIp,hardware,remark);
            if(res>0){
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, insideServerIp, "操作成功").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, insideServerIp, "操作失败").toJsonString();
            }
        }catch(Exception e){
            logger.error("[synVersion][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String checkInsideServerIp(String insideServerIp){
        String jsonStrResp = "";
        try {
            if(insideServerIp == null){
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
                return jsonStrResp;
            }
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.checkInsideServerIp(insideServerIp);
            if(res>0){
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR_INSIDEIP_EXIST, insideServerIp, "内网ip已存在").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, insideServerIp, "内网ip不存在").toJsonString();
            }
        }catch(Exception e){
            logger.error("[synVersion][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String getMainVersion(){
        String jsonStrResp = "";
        String versions;
        try {
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            versions = service.getMainVersion();
            if(versions==null){
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, versions, "主版本获取失败").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, versions, "主版本获取成功").toJsonString();
            }
        }catch(Exception e){
            logger.error("[synVersion][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String setMainVersion(String versions){
        versions = versions.substring(0,versions.lastIndexOf(','));
        String jsonStrResp = "";
        try {

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.setMainVersion(versions);
            if(res<0){
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, "主版本："+versions, "操作失败").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC,"主版本："+versions,"操作成功").toJsonString();
            }
        }catch(Exception e){
            logger.error("[setMainVersion][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String synVersionStatus(String versions){
        versions = versions.substring(0,versions.lastIndexOf(','));
        String jsonStrResp = "";
        try {

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.synVersionStatus(versions);
            if(res<0){
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, "主版本："+versions, "操作失败").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC,"主版本："+versions,"操作成功").toJsonString();
            }
        }catch(Exception e){
            logger.error("[synVersionStatus][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }


    @SkipInterfaceLog
    public String delServerCluster(String id) {
        String jsonStrResp = "";
        try {
            if(id == null){
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
                return jsonStrResp;
            }

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.delServerCluster(Integer.parseInt(id));
            if(res>0){
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, res, "操作成功").toJsonString();
            }else{
                jsonStrResp = Constants.getResponse(Constants.CODE_ERROR, res, "操作失败").toJsonString();
            }
        }catch(Exception e){
            logger.error("[delServerCluster][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }

        return jsonStrResp;
    }


    @SkipInterfaceLog
    public String clusterList(String clusterId, String clusterName, String status, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {

            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();
            if (StringUtils.isNotEmpty(clusterId)) {
                params.put("EQI_id", clusterId);
            }
            if (StringUtils.isNotEmpty(clusterName)) {
                params.put("LIKES_name", clusterName);
            }
            if (StringUtils.isNotEmpty(status)) {
                params.put("EQI_status", status);
            }

            if("all".equals(clusterName)){
                params.remove("LIKES_name");
            }

            if("all".equals(clusterId)){
                params.remove("EQI_id");
            }

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "addTime", "desc", true);
            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TServerClusterDataPage tServerClusterDataPage = service.getServerClusterData(page, tFilter);
            if (tServerClusterDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tServerClusterDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;

        }catch(Exception e){
            logger.error("[clusterList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    public String editServerCluster(String clusterId, String clusterName, String status, String serverList, String idc) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(clusterId)&&StringUtils.isEmpty(clusterName)&&StringUtils.isEmpty(status)&&StringUtils.isEmpty(idc)&&StringUtils.isEmpty(serverList)) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }

            String[] servers = serverList.split(",");
            com.iqiyi.pps.epg.api.model.web.sginal.TServerCluster thriftModel = new com.iqiyi.pps.epg.api.model.web.sginal.TServerCluster();
            thriftModel.setId(Integer.parseInt(clusterId));
            thriftModel.setName(clusterName);
            thriftModel.setIdc(idc);
            thriftModel.setStatus(Integer.parseInt(status));
            thriftModel.setUser(ServiceUtil.getOpUser());

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int res = service.updateServerCluster(thriftModel);
            List<String> list = new ArrayList<String>(Arrays.asList(servers));
            int mods = service.changeClusterServers(list,Integer.parseInt(clusterId));

            if (res > 0 && mods >0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, res, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, res, getCodeStr(ERROR_EXCEPTION));
            }
        } catch (Exception e) {
            logger.error("[EPG][API][editServerCluster][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String createServerCluster(String clusterName, String status, String serverList, String idc) {
        String jsonStrResp = "";
        try {
            if (StringUtils.isEmpty(clusterName)&&StringUtils.isEmpty(status)&&StringUtils.isEmpty(idc)&&StringUtils.isEmpty(serverList)) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
                return jsonStrResp;
            }
            String[] servers = serverList.split(",");
            com.iqiyi.pps.epg.api.model.web.sginal.TServerCluster thriftModel = new com.iqiyi.pps.epg.api.model.web.sginal.TServerCluster();
            thriftModel.setName(clusterName);
            thriftModel.setIdc(idc);
            thriftModel.setStatus(Integer.parseInt(status));
            thriftModel.setUser(ServiceUtil.getOpUser());

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            int entityId = service.createServerCluster(thriftModel);
            if (entityId > 0) {
                List<String> list = new ArrayList<String>(Arrays.asList(servers));
                int mods = service.setClusterServers(list,entityId);
                if(mods>0){
                    jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "修改成功");
                }else{
                    jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "修改失败");
                }
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, getCodeStr(entityId));
            }
        } catch (Exception e) {
            logger.error("[EPG][API][createServerCluster][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "发现异常");
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    public String IdcServerList(String idc, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;

            Map<String, String> params = new HashMap();

            if (StringUtils.isNotEmpty(idc)) {
                params.put("EQS_idc", idc);
            }
            if ("all".equals(idc)) {
                params.remove("EQS_idc");
            }

            params.put("EQI_serverType",12+""); //新收录
            params.put("NEQI_status", "-1");

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = null;
            page = new TPage(-1, pageNo, pageSize, "cluster", "asc", true);

            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TServerDataPage tServerDataPage = service.getServersData(page, tFilter);
            if (tServerDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tServerDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[IdcServerList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, e, "发现异常");
            return jsonStrResp;
        }
    }

    public String clusterSeverList(String cluster, String clusterName, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = 300;

            Map<String, String> params = new HashMap();

            if (StringUtils.isNotEmpty(cluster)) {
                params.put("EQI_cluster", cluster);
            }else{
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            if ("all".equals(cluster)) {
                params.remove("EQI_cluster");
            }

            params.put("EQI_serverType",12+"");//新收录
            params.put("NEQI_status", "-1");

            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = null;
            page = new TPage(-1, pageNo, pageSize, "updateTime", "desc", true);


            SginalWebService.Iface service = ServiceHelper.getThriftService(SginalWebService.Iface.class);
            TServerDataPage tServerDataPage = service.getServersData(page, tFilter);
            if (tServerDataPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tServerDataPage, null).toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "数据为空");
            }
            return jsonStrResp;
        } catch (Exception e) {
            logger.error("[clusterSeverList error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, e, "发现异常");
            return jsonStrResp;
        }
    }
}
